package com.zhongke.common.viewadapter.command

/**
 *@Date: 2022/2/11
 *@Time: 15:53
 *@Author:cxc
 *@Description:
 */
class BindingCommand<T> {
    private var bindingAction: BindingAction? = null
    private var bindingConsumer: BindingConsumer<T>? = null

    /**
     * 绑定事件的执行要求，true为执行，false不执行，不传默认为true
     */
    private var bindingRequire: BindingRequire? = null

    constructor(action: BindingAction) : this(action, null)

    /**
     * 带泛型参数的绑定事件
     * @param consumer BindingConsumer<T>
     * @constructor
     */
    constructor(consumer: BindingConsumer<T>) : this(consumer, null)

    constructor(action: BindingAction, require: BindingRequire?) {
        bindingAction = action
        bindingRequire = require
    }

    constructor(consumer: BindingConsumer<T>, require: BindingRequire?) {
        bindingConsumer = consumer
        bindingRequire = require
    }

    fun execute() {
        if (canExecute())
            bindingAction?.call()
    }

    /**
     *带参数的事件
     * @param t T
     */
    fun execute(t: T) {
        if (canExecute())
            bindingConsumer?.call(t)
    }

    /**
     * 是否能执行事件
     * @return Boolean true执行 false不执行
     */
    private fun canExecute(): Boolean {
        return bindingRequire?.checkRequire() ?: true
    }
}